//
//  yzmViewController.m
//  何五路
//
//  Created by _ADY on 15/6/26.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "yzmViewController.h"

@interface yzmViewController ()

@end

@implementation yzmViewController
@synthesize numString;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"手机快速注册";
        self.view.backgroundColor = bgColor;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    UILabel *title1Label = [[UILabel alloc] initWithFrame:CGRectMake(20,74, screenMySize.size.width-40, 25)];
    title1Label.text = [NSString stringWithFormat:@"请输入%@收到的短信验证码",numString];
    title1Label.font = [UIFont fontWithName:@"Arial" size:labelSize-2];
    title1Label.textColor = [UIColor grayColor];
    [self.view addSubview:title1Label];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    account = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, screenMySize.size.width-160, 40)];
    [account setFont:[UIFont systemFontOfSize:labelSize]];
    account.returnKeyType = UIReturnKeyDefault;
    [account setClearButtonMode:UITextFieldViewModeWhileEditing];
    account.delegate = self;
    account.keyboardType = UIKeyboardTypeNumberPad;
    account.layer.cornerRadius=3.0f;
    account.layer.masksToBounds=YES;
    account.backgroundColor=[UIColor whiteColor];
    account.layer.borderColor=[[UIColor grayColor]CGColor];
    account.layer.borderWidth= 1.0f;
    [account setPlaceholder:@"请输入验证码"];
    [self.view  addSubview:account];
    [account becomeFirstResponder];
    
    
    inButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [inButton setFrame:CGRectMake(screenMySize.size.width-130, 100, 100, 40)];
    [inButton setBackgroundImage:[UIImage imageNamed:@"enButton"] forState:UIControlStateNormal];
    inButton.enabled = NO;
    inButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize-1];
    [inButton addTarget:self action:@selector(inAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:inButton];
    
    
    cxLabel = [[UILabel alloc] initWithFrame:CGRectMake(screenMySize.size.width-130, 100, 100, 40)];
    cxLabel.font = [UIFont fontWithName:@"Arial" size:labelSize-1];
    cxLabel.textAlignment = 1;
    cxLabel.backgroundColor = [UIColor clearColor];
    cxLabel.textColor = [UIColor grayColor];
    [self.view addSubview:cxLabel];
    [self scPush];
    
    xButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [xButton setFrame:CGRectMake(15, 160, screenMySize.size.width-30, (screenMySize.size.width-30)*74/549)];
    [xButton setBackgroundImage:[UIImage imageNamed:@"enButton"] forState:UIControlStateNormal];
    [xButton setTitle:@"下一步" forState:UIControlStateNormal];
    [xButton setTitleColor:[UIColor colorWithRed:205/255.0 green:205/255.0 blue:205/255.0 alpha:1] forState:UIControlStateNormal];
    xButton.enabled = NO;
    
    [xButton addTarget:self action:@selector(xAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:xButton];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, xButton.frame.size.height+xButton.frame.origin.y+5, screenMySize.size.width-40, 25)];
    titleLabel.text = [NSString stringWithFormat:@"遇到问题？您可以联系客服。"];
    titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize-1];
    titleLabel.textColor = [UIColor grayColor];
    [self.view addSubview:titleLabel];
}

-(void)scPush
{
    [inButton setBackgroundImage:[UIImage imageNamed:@"enButton"] forState:UIControlStateNormal];
    [inButton setTitle:@"" forState:UIControlStateNormal];
    [inButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    inButton.enabled = NO;
    
    
    if (mTimer != nil) {
        [mTimer invalidate];
        mTimer = nil;
    }
    mInt = 120;
    cxLabel.text = [NSString stringWithFormat:@"重新发送(%d)",mInt];
    cxLabel.alpha = 1;
    mTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(setPush) userInfo:nil repeats:YES];
}

-(void)setPush
{
    mInt --;
    cxLabel.text = [NSString stringWithFormat:@"重新发送(%d)",mInt];
    if (mInt == 0)
    {
        [inButton setBackgroundImage:[UIImage imageNamed:@"enButtons"] forState:UIControlStateNormal];
        [inButton setTitle:@"重新获取" forState:UIControlStateNormal];
        [inButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        inButton.enabled = YES;
        
        cxLabel.alpha = 0;
        if (mTimer != nil) {
            [mTimer invalidate];
            mTimer = nil;
        }
    }
}

-(void)inAction
{
    [self scPush];
}

-(void)mButon
{
    if (account.text.length!=0)
    {
        [xButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        xButton.enabled = YES;
        [xButton setBackgroundImage:[UIImage imageNamed:@"enButtons"] forState:UIControlStateNormal];
    }
    else
    {
        [xButton setTitleColor:[UIColor colorWithRed:205/255.0 green:205/255.0 blue:205/255.0 alpha:1] forState:UIControlStateNormal];
        xButton.enabled = NO;
        [xButton setBackgroundImage:[UIImage imageNamed:@"enButton"] forState:UIControlStateNormal];
    }
}

-(void)xAction
{
    
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

#pragma mark -
#pragma mark UITextFieldDelegate
//限制输入长度 by zwb 2011.9.6
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == account)
    {
        NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if (text.length!=0)
        {
            [xButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            xButton.enabled = YES;
            [xButton setBackgroundImage:[UIImage imageNamed:@"enButtons"] forState:UIControlStateNormal];
        }
        else
        {
            [xButton setTitleColor:[UIColor colorWithRed:205/255.0 green:205/255.0 blue:205/255.0 alpha:1] forState:UIControlStateNormal];
            xButton.enabled = NO;
            [xButton setBackgroundImage:[UIImage imageNamed:@"enButton"] forState:UIControlStateNormal];
        }
        
    }
    return YES;
    
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self mButon];
    
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
